package com.notepad.ui;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.notepad.R;
import com.notepad.domain.UserSetting;
import com.notepad.util.Constant;
import com.notepad.util.FileUtil;

/**
 * Created by jiamengyu on 1/24/2015.
 */
public class SettingActivity extends Activity {
    private ActionBar mActionBar;
    private ToggleButton mToggleButton;
    private FileUtil mFileUtil;
    private UserSetting mUserSetting;
    private EditText mPWEdittext;
    private EditText mRePWEdittext;
    private Button mSetPasswordButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        initActionBar();
        init();
        setListener();
    }

    private void setListener() {


        mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mToggleButton.setChecked(isChecked);
                if(isChecked == true){
                    viewDialog();
                    Constant.ALLOW_LOCK = true;
                }else{
                    Constant.ALLOW_LOCK = false;
                }
            }
        });

    }

    private void init() {
        mFileUtil = new FileUtil(SettingActivity.this);
        mToggleButton = (ToggleButton) findViewById(R.id.lock_togglebutton);
        mUserSetting = new UserSetting();
    }

    private void initActionBar() {
        mActionBar = getActionBar();
        ColorDrawable drawable = new ColorDrawable(getResources().getColor(
                R.color.pink_normal));
        mActionBar.setBackgroundDrawable(drawable);
        mActionBar.setDisplayHomeAsUpEnabled(true);
        mActionBar.setHomeButtonEnabled(true);
        mActionBar.setDisplayShowHomeEnabled(true);
        mActionBar.setTitle(getString(R.string.setting));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    public void viewDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
        LayoutInflater factory = LayoutInflater.from(SettingActivity.this);
        View view = factory.inflate(R.layout.setpassword, null);
        // 设定显示的View
        builder.setView(view);
        final AlertDialog dialog = builder.create();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.myAnimationstyle);
        dialog.show();

        mPWEdittext = (EditText) view
                .findViewById(R.id.dialog_password_EditText_id);
        mRePWEdittext = (EditText) view
                .findViewById(R.id.dialog_re_password_EditText_id);
        mSetPasswordButton = (Button) view.findViewById(R.id.dialog_set_pw_btn_id);
        mSetPasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            String num = mPWEdittext.getText().toString();
            String reNum = mRePWEdittext.getText().toString();
                if(num == null || "".equals(num) || reNum == null || "".equals(reNum) ){
                    Toast.makeText(SettingActivity.this,R.string.password_null_tip,Toast.LENGTH_SHORT).show();
                }else{
                    if(!num.equals(reNum)){
                        Toast.makeText(SettingActivity.this,R.string.password_wrong_tip,Toast.LENGTH_SHORT).show();
                    }

                        Constant.LOCK_PASSWORD = num;
                        Constant.ALLOW_LOCK = true;
                        mUserSetting.setPassword(Constant.LOCK_PASSWORD);
                        mUserSetting.setIsLock(Constant.ALLOW_LOCK);
                        mFileUtil.SaveUserSetting(mUserSetting);
                        Toast.makeText(SettingActivity.this,R.string.password_set_success_tip,Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                }

            }
        });


    }
}
